// Copyright 2024 Tvux
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _055

// canJump
// 假设当前下标的值为x，那么在当前下标处，最大能到达的距离就是i+x
// 只需要遍历nums数组，然后不断更新最大能到达距离，跟当前下标比
// 如果当前下标大于最大能到达距离，则说明无法到达这个下标，到不了这个下标，就更不可能到终点了
func canJump(nums []int) bool {
	maxJump := 0
	for i := 0; i < len(nums); i++ {
		if i > maxJump {
			return false
		} else {
			maxJump = max(maxJump, i+nums[i])
		}
	}
	return true
}
